import Toast from '@vant/weapp/toast/toast';
const app = getApp()
const api = app.globalData.api
Page({

  /**
   * 页面的初始数据
   */
  data: {
    clockTime:'-',
    active:0,
    time:'',
    timer:'',
    show:false,
    menu:[],
    weeArray:['日','一','二','三','四','五','六'],
    dayArr:[],
    date_title:'',
    state:'缺卡',
    selected_Date:'',
    selected_month:'',
    formate_selected_month:'',
    formate_selected_date:'',
    today:'',//当前选中的第几个日期
    latitude:'',//纬度
    longitude:'',//经度
    clockPageInfo:{},
    startSignTime:'',
    endSignTime:'',
  },

  /***打卡**/
  clockSign(){
    let that = this;
    return;
    wx.getLocation({
      type: 'gcj02',
      success (res) {
        that.setData({
          latitude: res.latitude,
          longitude:res.longitude
        })
        api.curlPost('?cla=clockApp&fun=sign',{lat:that.data.latitude,lng:that.data.longitude}).then((res)=>{
          that.setData({
            show:true,
            clockTime:res.data.clockTime
          })
        })
      }
     })
  },
  /***关闭弹出层 */
  onClose(e){
    this.requestData();
    this.setData({
      show:false
    })
  },
  /**tabar切换 */
  onChange(event){
    let index = event.detail;
    this.setData({
      active: index
    });
    this.requestData();
  },
  //获取当前时分秒
  getTime(){
    this.setData({
      time:api.formatDateTime(new Date(),'HH:mm:ss')
    })
  },
  chooseDate(e){
    this.setData({
      formate_selected_date:e.currentTarget.dataset.date
    })
    this.requestData();
  },
  changeMonth(e){
    let type = e.currentTarget.dataset.type;
    let month = this.data.formate_selected_month;
  
    if(type=='pre'){
      month=api.getPreMonth(api.formatDateCRM('Y/m',month));
    }else if(type=='next'){
      month=api.getNextMonth(api.formatDateCRM('Y/m',month));
    }
    this.requestData(month);
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    return;
    wx.getLocation({
      type: 'gcj02',
      success (res) {
        that.setData({
          latitude: res.latitude,
          longitude:res.longitude
        })
        that.requestData();
      }
     })
  },

  /**请求数据 */
  requestData(month=''){
    let that = this;
    if(this.data.active==0){
      //打卡
      api.curlPost('?cla=clockApp&fun=clockPage',{lat:this.data.latitude,lng:this.data.longitude}).then((res)=>{
        that.setData({
          clockPageInfo:res.data.result
        })
      })
    }else if(this.data.active == 1){
      api.curlPost('?cla=clockApp&fun=apply').then((res)=>{
        that.setData({
          menu:res.data.result
        })
      })
    }else if(this.data.active == 2){
      let para = {
        date:that.data.formate_selected_date,
        month:month
      }
      api.curlPost('?cla=clockApp&fun=statistics',para).then((res)=>{
        that.setData({
          date_title:res.data.result.formatDate,
          state:res.data.result.state,
          dayArr:res.data.result.list,
          selected_month:api.formatDateCRM('Y年m月',res.data.result.month),
          formate_selected_month:api.rowTocol(res.data.result.month),
          formate_selected_date:res.data.result.date,
          startSignTime:res.data.result.startSignTime,
          endSignTime:res.data.result.endSignTime,
        })
      })
    }
  },

  /**
   * 重新打卡
   */
  reSign(e){
    let that = this;
    let id = e.currentTarget.dataset.id;
    return;
    wx.showModal({
      title: '提示',
      content: '是否确定重新打卡？',
      success (res) {
        if (res.confirm) {
          wx.getLocation({
            type: 'gcj02',
            success (res) {
              that.setData({
                latitude: res.latitude,
                longitude:res.longitude
              })
              api.curlPost('?cla=clockApp&fun=reSign',{lat:that.data.latitude,lng:that.data.longitude,id:id}).then((res)=>{
                that.requestData();
                Toast(res.warn);
              })
            }
           })
        }
      }
    })
    
   
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that = this;
    if(this.data.clockPageInfo.clockType !=2){
      that.getTime();
      let timer = setInterval(function(){
        that.getTime();
      },1000);
      this.setData({
        timer
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    clearInterval(this.data.timer)
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})